<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class NoticeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function add()
    {

        return view('admin.Notice.add',['title'=>'公告添加']);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $num = $request -> input('num',10);
        $keywords = $request -> input('keywords','');
        $data = DB::table('notice') -> where('name','like','%'.$keywords.'%') -> paginate($num) ;

        // $com = DB::table('notice') -> select() -> get();


        return view('admin.Notice.index',['data' => $data ,'request' => $request -> all(), 'title' => '用户列表']);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function doadd(Request $request)
    {
        $data = $request -> except('_token');
        $time = Date('Y-m-d H-i-s');
        $data['created_at'] = $time;
        
        $res = DB::table('notice') -> insert([$data]);
        if($res)
         {
            return back() -> with(['info' => '添加成功']);
         }else
         {
            return back() -> with(['info' => '添加失败']);
         }

        
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete($id)
    {
        $res = DB::table('notice') -> where('id',$id) -> delete();
        if($res)
         {
            return back() -> with(['info' => '删除成功']);
         }else
         {
            return back() -> with(['info' => '删除失败']);
         }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $list = DB::table('notice') -> where('id',$id) -> first();
        
        return view('admin.Notice.edit',['title'=>'编辑公告','list'=>$list]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $data = $request -> except('_token');
        $res = DB::table('notice') -> where('id',$data['id']) -> update(['name'=>$data['name'],'Noticetext'=>$data['Noticetext']]);
        if($res)
         {
          return redirect('/admin/Notice/index') -> with(['info' => '修改成功']);
         }else
         {
          return back() -> with(['info' => '修改失败']);
         }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */

}
